﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBearLite;
using SzCourt.Logic.Common;
using SzCourt.Logic.NBL;
using KMFrame.Util;
using KMFrame.Core;
using SzCourt.Logic.NBL.DBEntities;

namespace SzCourt.Logic.BLL
{
    public class UserInfo : SzCourtLogic
    {
        public bool CreateUser(string username, string password, string userkey, Byte UserType)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(SZCourtColumns.pUserInfo)
                .AddColumn(SZCourtColumns.pUserInfo.UserName, username)
                .AddColumn(SZCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password,32))
                .AddColumn(SZCourtColumns.pUserInfo.IdentityCard, userkey)
                .AddColumn(SZCourtColumns.pUserInfo.UserGUID, Guid.NewGuid())
                .AddColumn(SZCourtColumns.pUserInfo.UserType, UserType);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        /// <summary>
        /// 创建记者之家用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="userkey"></param>
        /// <param name="UserType"></param>
        /// <returns></returns>
        public bool CreateReporterUser(string username, string password, string userkey,string phone,string email, Byte UserType)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(SZCourtColumns.pUserInfo)
                .AddColumn(SZCourtColumns.pUserInfo.UserName, username)
                .AddColumn(SZCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password, 32))
                .AddColumn(SZCourtColumns.pUserInfo.IdentityCard, userkey)
                .AddColumn(SZCourtColumns.pUserInfo.Phone, phone)
                .AddColumn(SZCourtColumns.pUserInfo.Email, email)
                .AddColumn(SZCourtColumns.pUserInfo.UserGUID, Guid.NewGuid())
                .AddColumn(SZCourtColumns.pUserInfo.UserType, UserType);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public PUserInfo Login(string username, string password)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.pUserInfo.UserID, 
                SZCourtColumns.pUserInfo.UserName,
                SZCourtColumns.pUserInfo.UserGUID,
                SZCourtColumns.pUserInfo.UserType
            };
            return NBLData.Select(SZCourtColumns.pUserInfo,selectColumns)
                .Where(SZCourtColumns.pUserInfo.UserName == username && SZCourtColumns.pUserInfo.UserPWD == SecurityHelper.MD5(password, 32))
                .ToSingleObject<PUserInfo>();
        }

        /// <summary>
        /// 删除记者之家用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DelReporterUser(int userID)
        {
            bool result = false;
            DeleteSqlSection deleteUserInfo = NBLData.Delete(SZCourtColumns.pUserInfo)
                 .Where(SZCourtColumns.pUserInfo.UserID == userID);
            int ri = deleteUserInfo.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 获取记者之家用户列表信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PageResult GetUserInfoList(PageParam condition, string keyword)
        {
            PageResult result = new PageResult();

            WhereClip where = SZCourtColumns.pUserInfo.UserType == UserTypes.REPORTS;
            if (!string.IsNullOrEmpty(keyword))
            {
                SZCourtColumns.pUserInfo.UserName.Like("%" + keyword + "%");
            }
            result.ResultList = NBLData
                .Select(SZCourtColumns.pUserInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.pUserInfo.UserID)
                .OrderBy(SZCourtColumns.pUserInfo.UserID.Desc)
                .ToList<pUserInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pUserInfo, SZCourtColumns.pUserInfo.UserID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }
    }

    public class PUserInfo
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public System.Guid UserGUID { get; set; }
        public Byte? UserType { get; set; }
    }

    /// <summary>
    /// 用户类型
    /// </summary>
    public static class UserTypes
    {
        /// <summary>
        /// 普通帐户
        /// </summary>
        public static Byte GENERAL
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 记者之家用户
        /// </summary>
        public static Byte REPORTS
        {
            get
            {
                return 2;
            }
    }
    }
}
